/**
 * @file dev_driver.c
 * @brief 
 * 
 * @author 1024Tech (店铺链接：https://1024tech.taobao.com/)
 * @version 1.0
 * @date 2023-06-08
 * 
 * @copyright Copyright (c) 2023  OpenSir Robot Tech.co, Ltd
 * 
 * @par 修改日志:
 * <table>
 * <tr><th>Date       <th>Version <th>Author  <th>Description
 * <tr><td>2023-05-08 <td>1.0     <td>Mr.lin     <td>Release
 * </table>
 */

#include "dev_driver.h"

#define PWMA_TIM    DRV_TIM2    /* PWMA对应定时器 */
#define A_CHANNEL   TIM_CH1     /* PWMA对应定时器通道 */
#define PWMB_TIM    DRV_TIM2    /* PWMB对应定时器 */
#define B_CHANNEL   TIM_CH2     /* PWMB对应定时器通道 */

driverDevConfig dev_driver;

/* 驱动模块引脚配置 */
driverDevConfig driver_list[IO_MAX] =
{
    {"AIN1", { (gpioType_e)DRV_GPIOA, (gpioPin_e)DRV_PIN_4 , (drvPinState_e)0}},
    {"AIN2", { (gpioType_e)DRV_GPIOA, (gpioPin_e)DRV_PIN_5 , (drvPinState_e)0}},
    {"BIN1", { (gpioType_e)DRV_GPIOB, (gpioPin_e)DRV_PIN_4 , (drvPinState_e)0}},
    {"BIN2", { (gpioType_e)DRV_GPIOB, (gpioPin_e)DRV_PIN_3 , (drvPinState_e)0}},
    {"STBY", { (gpioType_e)DRV_GPIOA, (gpioPin_e)DRV_PIN_12 , (drvPinState_e)0}}
};
/**
 * @brief 初始化驱动模块
 * @note  使能，启动定时器PWM
 */
void dev_driver_init(void)
{
    for(int i = 0; i < IO_MAX; i++){
        drv_gpio_write(driver_list[i].gpio, driver_list[i].gpio.ste);
    }
}

/**
 * @brief 设置驱动模块设备状态
 * @param  pin_id   : 驱动引脚ID
 * @param  state    : 设置的状态
 */
static void dev_driver_state_control(driverId_e pin_id, driverState_e state)
{
    drv_gpio_write(driver_list[pin_id].gpio, (drvPinState_e)state); 
}

/**
 * @brief  驱动模块使能控制
 * @param  state : HIGH使能，LOW失能
 */
void dev_driver_enable(driverState_e state)
{
    dev_driver_state_control(STBY,state);
}

/**
 * @brief  驱动模块A通道方向配置
 * @param  dir : CW顺时针，CCW逆时针
 */
void dev_driver_chA_config(driverDir_e dir)
{
    if( CW == dir ){
        dev_driver_state_control(AIN1,LOW);
        dev_driver_state_control(AIN2,HIGH);        
    }else{
        dev_driver_state_control(AIN2,LOW);
        dev_driver_state_control(AIN1,HIGH);  
    }
}
/**
 * @brief  驱动模块B通道方向配置
 * @param  dir : CW顺时针，CCW逆时针
 */
void dev_driver_chB_config(driverDir_e dir)
{
    if( CW == dir ){
        dev_driver_state_control(BIN1,LOW);
        dev_driver_state_control(BIN2,HIGH);        
    }else{
        dev_driver_state_control(BIN2,LOW);
        dev_driver_state_control(BIN1,HIGH);  
    }
}
/**
 * @brief  驱动模块A通道PWM配置
 * @param  duty :占空比
 */
void dev_driver_pwmA_duty(uint32_t duty)
{
    drv_pwm_set_dutycycle( PWMA_TIM , A_CHANNEL , duty);
}

/**
 * @brief  驱动模块B通道PWM配置
 * @param  duty :占空比
 */
void dev_driver_pwmB_duty(uint32_t duty)
{
    drv_pwm_set_dutycycle( PWMB_TIM , B_CHANNEL , duty);
}
